package cn.iocoder.yudao.module.trade.controller.admin.depositorderlackbottles;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.depositorderlackbottles.vo.*;
import cn.iocoder.yudao.module.trade.service.depositorderlackbottles.DepositOrderLackBottlesService;

@Tag(name = "管理后台 - 欠瓶记录")
@RestController
@RequestMapping("/trade/lack-bottles")
@Validated
public class DepositOrderLackBottlesController {

    @Resource
    private DepositOrderLackBottlesService depositOrderLackBottlesService;

    @PostMapping("/create")
    @Operation(summary = "创建欠瓶记录")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-lack-bottles:create')")
    public CommonResult<Long> createDepositOrderLackBottles(@Valid @RequestBody DepositOrderLackBottlesSaveReqVO createReqVO) {
        return success(depositOrderLackBottlesService.createDepositOrderLackBottles(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新欠瓶记录")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-lack-bottles:update')")
    public CommonResult<Boolean> updateDepositOrderLackBottles(@Valid @RequestBody DepositOrderLackBottlesSaveReqVO updateReqVO) {
        depositOrderLackBottlesService.updateDepositOrderLackBottles(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除欠瓶记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-lack-bottles:delete')")
    public CommonResult<Boolean> deleteDepositOrderLackBottles(@RequestParam("id") Long id) {
        depositOrderLackBottlesService.deleteDepositOrderLackBottles(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得欠瓶记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-lack-bottles:query')")
    public CommonResult<DepositOrderLackBottlesRespVO> getDepositOrderLackBottles(@RequestParam("id") Long id) {
        DepositOrderLackBottlesRespVO depositOrderLackBottles = depositOrderLackBottlesService.getDepositOrderLackBottles(id);
        return success(depositOrderLackBottles);
    }

    @GetMapping("/page")
    @Operation(summary = "获得欠瓶记录分页")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-lack-bottles:query')")
    public CommonResult<PageResult<DepositOrderLackBottlesRespVO>> getDepositOrderLackBottlesPage(@Valid DepositOrderLackBottlesPageReqVO pageReqVO) {
        PageResult<DepositOrderLackBottlesRespVO> pageResult = depositOrderLackBottlesService.getDepositOrderLackBottlesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepositOrderLackBottlesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出欠瓶记录 Excel")
    @PreAuthorize("@ss.hasPermission('trade:deposit-order-lack-bottles:export')")
    @OperateLog(type = EXPORT)
    public void exportDepositOrderLackBottlesExcel(@Valid DepositOrderLackBottlesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepositOrderLackBottlesRespVO> list = depositOrderLackBottlesService.getDepositOrderLackBottlesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "欠瓶记录.xls", "数据", DepositOrderLackBottlesRespVO.class,
                       list);
    }

}